<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <span>重置密码</span>
    </div>
    <!-- 表单 -->
    <el-form
      :model="pwdForm"
      :rules="pwdFormRules"
      ref="pwdFormRef"
      label-width="100px"
    >
      <el-form-item label="原密码" prop="oldPwd">
        <el-input
          type="password"
          class="input"
          v-model="pwdForm.oldPwd"
        ></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="newPwd">
        <el-input
          type="password"
          class="input"
          v-model="pwdForm.newPwd"
        ></el-input>
      </el-form-item>
      <el-form-item label="确认新密码" prop="rePwd">
        <el-input
          type="password"
          class="input"
          v-model="pwdForm.rePwd"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitFn">修改密码</el-button>
        <el-button @click="resetFn">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
import { updatePwdAPI } from '@/api'
export default {
  name: 'UserPwd',
  data () {
    const isOldPwdFn = (rule, value, callback) => {
      if (value === this.pwdForm.oldPwd) {
        callback(new Error('新密码不能和原密码相同！'))
      } else {
        callback()
      }
    }
    const samePwdFn = (rule, value, callback) => {
      if (value !== this.pwdForm.newPwd) {
        callback(new Error('两次输入的密码不一样！'))
      } else {
        callback()
      }
    }
    return {
      pwdForm: {
        oldPwd: '',
        newPwd: '',
        rePwd: ''
      },
      pwdFormRules: {
        oldPwd: [
          { required: true, message: '请输入原密码！', trigger: 'blur' },
          {
            pattern: /^\S{6,15}$/,
            message: '密码长度必须是6-15位的非空字符串',
            trigger: 'blur'
          }
        ],
        newPwd: [
          { required: true, message: '请输入新密码！', trigger: 'blur' },
          {
            pattern: /^\S{6,15}$/,
            message: '密码长度必须是6-15位的非空字符串',
            trigger: 'blur'
          },
          { validator: isOldPwdFn, trigger: 'blur' }
        ],
        rePwd: [
          { required: true, message: '请再次输入新密码！', trigger: 'blur' },
          {
            pattern: /^\S{6,15}$/,
            message: '密码长度必须是6-15位的非空字符串',
            trigger: 'blur'
          },
          { validator: samePwdFn, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submitFn () {
      this.$refs.pwdFormRef.validate(async valid => {
        if (valid) {
          // 校验通过
          const { data: res } = await updatePwdAPI(this.pwdForm)
          // console.log(res)
          if (res.code === 1) return this.$message.warning('原密码错误！更改失败！')
          this.$message.success('修改成功！')
          this.$store.dispatch('getUserInfoActions')
        } else {
          return false
        }
      })
    },
    resetFn () {
      this.$refs.pwdFormRef.resetFields()
    }
  }
}
</script>

<style scoped lang="less">
.input {
  width: 20%;
}
</style>
